/**
 *@Description 订单首页
 *@Author zhangnf
 *@Date 2015-09-23
 */
var orderCtrl  = new XFApp.client();
XFApp.client.prototype.order = {
	successCode : "10000",
	page : null,
	urlItems : {
		queryByPageURL : basePath+"/userCenter/order/queryPage",
		delOrderURL : basePath+"/userCenter/order/delOrder",
		reSendOrderSingleURL: basePath+"/userCenter/order/submit"
	},
	queryPage : function(){
		_this = this;
		var params = _this.formatParam($('#searchForm').serialize());
		if(!!_this.page){
			_this.page.page("remote",0,params);
		}else{
		_this.page = $("#page").page({
			pageSize : 10,
			remote: {
				url: _this.urlItems.queryByPageURL,
		        params: params, 
		        success: function (data, pageIndex) {
		        	if(data.code==_this.successCode){
		        		var pageResult = data.result;
		        		if(null == data 
								|| null == data.result 
									|| null == data.result.itemList
										|| data.result.itemList.length < 1){
		        			$("#page_grid").html('<tr><td align="center" colspan="11">暂无数据</td></tr>');
		        		}else{
		        			$("#page_grid").html($("#dataTmp").render(pageResult.itemList));
		        		}
		        	}else{
		        		window.webDialogUtil.alert("提示","操作失败");
		        	}
		        },
		        error : function(){
		        	window.webDialogUtil.alert("提示","操作失败");
		        }
			},
			firstBtnText : '首页',
			lastBtnText : '尾页',
			prevBtnText : '上一页',
			nextBtnText : '下一页',
			showInfo : true,
			showJump : true,
			jumpBtnText : '跳转',
			showPageSizes : true,
			infoFormat : '共{pageCount}页,{total}项'
		});
		}
	},
	delOrderDialog : function(id,orderCode){
		_this = this;
		webAlert({
            title:'提示',
            content:'确定要删除 '+orderCode+' 吗?',
            opacity:.5,
            bg:"gray",
            width:300,
            height:10,
            button:[
                    {
                    name:"确定",
                    callback:function(){
                    	_this.delOrder(id);
                    	this.close();
                    }
                    },{
                    name:"取消",
                    callback:function(){
                  	  this.close();
                    }
                    }]
            });
	},
	delOrder : function(id){
		_this = this;
		$.ajax({
		    url:_this.urlItems.delOrderURL,
		    data:{id:id},  
		    type:'post',  
		    cache:false,  
		    dataType:'json',  
		    success:function(data) {
		    	window.webDialogUtil.alert("提示",data.msg);
		    	if(true == data.isSuccess){
		    		orderCtrl.order.queryPage();
		    	}
		     },  
		     error : function() {
		    	 window.webDialogUtil.alert("提示","操作失败");
		     }  
		});
	},
	formatParam: function(param){
		var newParam = param.replace(/\+/g," ");
		newParam = decodeURIComponent(newParam);
		return newParam;
	},
	dataFormat : function(timeLong){
		if(null == timeLong
				|| "" == timeLong){
			return "";
		}
		return new Date(timeLong*1000).format("yyyy-MM-dd hh:mm:ss")
	},
	getQueryParam: function(){
		var arr = $('#searchForm').serializeArray();
		var param = "";
		for( var obj in arr){
			if(arr[obj].value){
				param += arr[obj].name+'='+arr[obj].value+'&'
			}else{
				param += arr[obj].name+'='+null+'&'
			}
		}
		return param.substring(0,param.length-1);
	},
	//单独重新发送或改单订单
	singleId: '',
	modifymark: '',
	reSendOrderSingle: function(id,status){
		var _this = this;
		_this.singleId = id;
		_this.modifymark = status;
		webAlert({
            title:'提示',
            content:'是否确认订单信息或回执信息有误并重新发送。',
            opacity:.5,
            bg:"gray",
            width:300,
            height:10,
            button:[
                    {
                    name:"确定",
                    callback:function(){
                    	_this.doSendOrder();
                    	this.close();
                    }
                    },{
                    name:"取消",
                    callback:function(){
                  	  this.close();
                    }
                    }]
         });
	},
	//改单
	doSendOrder: function(){
		var _this = this;
		$.ajax({
		    url:_this.urlItems.reSendOrderSingleURL,
		    data:{id:_this.singleId,modifymark:_this.modifymark},  
		    type:'post',  
		    cache:false,  
		    dataType:'json',  
		    success:function(data) {
		    	if(true == data.isSuccess  && data.code==10000){
		    		window.webDialogUtil.toast({
						type: 'success',
						title: '发送成功'
					});
		    	}else{
		    		window.webDialogUtil.toast({
						type: 'error',
						title: data.msg
					});
		    		//window.webDialogUtil.alert("提示",data.msg);
		    	}
		    	
		    	
		     },  
		     error : function() {
		    	 window.webDialogUtil.alert("提示","操作失败");
		     }  
		});
	},
	event : function(){
		_this = this;
		
		$("#searchButton").on("click",function(){
			orderCtrl.order.queryPage();
		});
		
		$("#orderDocketStatus").on('onSetSelectValue', function (e, keyword) {  
        	$('#status').attr('value',keyword.id); 
    	})
	},
};
//var stats_currencys =  dictCtrl.dictionary.translate("currency");
//var contrys = dictCtrl.dictionary.translate("nation");
$.views.converters({
	ftime : function(value) {
		return orderCtrl.order.dataFormat(value) ;
	},
	fdkstatus : function(value){
		return dksCtrl.dockerstatus.get(value);
	},
	fmoney : function(value){
		if(null == value
				|| "" == value){
			return "";
		}
		return orderCtrl.fmoney(value);
	},
	fmoneyUnit : function(value){
		if(!value){
			return "";
		}
		for(index in stats_currencys){
			var currencys = stats_currencys[index];
			if(currencys.dictCode == value){
				return currencys.dictName;
			}
		}
		return  "未知类型("+value+")";
	},
	fcontry: function (value){
		if(!value){
			return "";
		}
		for(index in contrys){
			var contry = contrys[index];
			if(contry.dictCode == value){
				return contry.dictName;
			}
		}
	}
});
$(function(){
	dictCtrl.dictionary.load("orderNewStatus","docketStatusHidden","orderDocketStatus");
	orderCtrl.order.event();
	orderCtrl.order.queryPage();
});